namespace UpgradeLoader
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            test.UpgradeFinishEvent += Test_UpgradeFinishEvent;
            oneManager.UpgradeFinishEvent += Test_UpgradeFinishEvent;
            oneManager.DownCenter.DownProgressEvent += DownCenter_DownProgressEvent;
            oneManager.DownCenter.DownloadStartEvent += DownCenter_DownloadStartEvent;
        }

        private void DownCenter_DownloadStartEvent(long obj)
        {
            if(this.InvokeRequired)
            {
                this.Invoke((Action)(() => { this.pBar.Maximum = (int)obj; })); 
            }
            else
            {
                this.pBar.Maximum = (int)obj;
            }
        }

        private void DownCenter_DownProgressEvent(long total, long downladed, int offset)
        {
            if (this.InvokeRequired)
            {
                this.Invoke((Action)(() => { this.pBar.Value = (int)(offset + downladed); }));
            }
            else
            {
                this.pBar.Value = (int)(offset + downladed);
            }
        }

        private void Test_UpgradeFinishEvent(bool obj)
        {
            test.UpgradeFinishEvent -= Test_UpgradeFinishEvent;
            oneManager.UpgradeFinishEvent -= Test_UpgradeFinishEvent;
            if(this.InvokeRequired)
            {
                this.Invoke((Action)(() => { this.Close(); }));
            }
            else
            {
                this.Close();
            }
            
        }

        string upserverPath = @"D:\iisweb\mainUpgrade\";
        Upgrademanager test = new Upgrademanager();
        OneFileUpgradeManager oneManager = new OneFileUpgradeManager();
        private void btn_CreateServerUpgradeFile_Click(object sender, EventArgs e)
        {
            // var upgrade = Upgrademanager.CreateTestInstalledSetting();
            // upgrade.VersionInfo.CurrentVersion = "1.0.0.2";
            //upgrade= test.CreateUpdatePackages(upgrade, upserverPath+ "upgradedir");
            // Util.SerializeXml(upgrade, upserverPath + "UpgradeSettings.xml");
            // MessageBox.Show("ok");
            //test.StartUpgrade();
            oneManager.StartUpgrade();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            notifyIcon1.Visible = true;
           
            this.WindowState = FormWindowState.Minimized;
            this.Hide();
           
        }
    }
}
